<template>
    <view class="content updatePwd">
        <view>
            <view class="updatePwd-input">
                <input v-model="oldPassword" type="password" :placeholder="$t('app.message.enterOldPwd')" />
            </view>
            <view class="updatePwd-input">
                <input v-model="newPassword" type="password" :placeholder="$t('app.message.enterNewPwd')" />
            </view>
            <view class="updatePwd-input">
                <input v-model="confirmPassword" type="password" :placeholder="$t('app.message.enterNewPwdAgain')" />
            </view>
        </view>
        <view class="btnDiv">
            <button @click="modifyPassword">{{$t('baseCommon.buttons.submit')}}</button>
        </view>
    </view>
</template>

<script>
	import { modifyPassword } from '@/api/platform/common.js'
	import { notice } from '@/utils/common.js'
	
    export default {
        data() {
            return {
				oldPassword: '',
				newPassword: '',
				confirmPassword: ''
            }
        },
        methods: {
			modifyPassword() {
				var valid = this.validateForm();
				if(!valid){
					return;
				}
				let userIds = []
				userIds.push(uni.getStorageSync("userId"))
				const params = {
					newPassword: this.newPassword,
					primitivePassword: this.oldPassword,
					repeatPassword: this.confirmPassword,
					reset: 0,
					userIds: userIds
				}
				modifyPassword(params).then(res => {
					if (res) {
						uni.showToast({
							title: this.$t('app.message.pwdChangeSuccess'),
							duration: 2000
						});
						setTimeout(function(){
							uni.clearStorage();
							uni.reLaunch({
								url: '/pages/platform/login/index'
							});
						},2000)
					} else {
						notice.toast(this.$t('app.message.pwdChangeFailed'))
					}
				})
			},
			validateForm() {
				var flag = true;
				
				if (!this.oldPassword) {
					notice.toast(this.$t('app.message.enterOldPwd'))
					flag = false;
				}
				if (!this.newPassword) {
					 uni.showToast({
					    title: this.$t('app.message.enterNewPwd'),
					 	icon: 'none',
					    duration: 2000
					 });
					flag = false;
				}
				if (!this.confirmPassword) {
					notice.toast(this.$t('app.message.enterNewPwdAgain'))
					flag = false;
				}
				if (this.newPassword !== this.confirmPassword) {
					notice.toast(this.$t('app.message.twoInputsInconsistent'))
					flag = false;
				}
				return flag;
			}
        }
    }
</script>

<style lang="scss">
    .updatePwd-input{
    	border-bottom: 1px solid #666;
    	padding: 12px 10px;
    	margin: 0 10px;
    }
	
	.btnDiv {
	  padding: 5px 10px;
	  margin-top: 20px;
	}
	
	.btnDiv button {
		background-color: #1E90E6;
		color: #fff;
	}
</style>
